package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.dayuan.bean.Goods;
import com.dayuan.constant.ConstantCode;
import com.dayuan.service.GoodsService;
import com.dayuan.utils.pageHelper;


@Controller
@RequestMapping("/goods")
public class GoodsController {

	private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

	@Resource
	private GoodsService goodsService;

	/**
	 * 分页查询所有商品信息
	 * 
	 * @param pageIndex
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/goodsList.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView goodsList(Integer pageIndex, ModelMap modelMap) {
		try {

			if (pageIndex == null) {
				pageIndex = 1;
			}

			int totalCount = goodsService.selectCountGoodsByParam();
			pageHelper pageHelper = new pageHelper(pageIndex, totalCount);
			List<Goods> goodsList = goodsService.selectCountGoodsByParamForPage(pageHelper.getStartIndex(),
					pageHelper.getPageNum());

			modelMap.addAttribute("goods", goodsList);
			modelMap.addAttribute("pageHelper", pageHelper);

			ModelAndView modelAndView = new ModelAndView("goodsList", modelMap);

			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * 通过商品名称关键字 模糊查询商品信息集合,
	 * 若传入值为null，则查询出所有商品信息
	 * 添加分页功能
	 * 
	 * @param goodsName
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getGoodsByName.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView getGoodsByName(String goodsName,Integer pageIndex,  ModelMap modelMap) {
		try {

			List<Goods> goodsList = goodsService.selectGoodsByParam(goodsName);

			modelMap.addAttribute("goodsList", goodsList);

			ModelAndView modelAndView = new ModelAndView("goodsList", modelMap);

			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * 通过商品id 精确查询商品信息
	 * 
	 * @param goodsId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getGoodsById.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView getGoodsById(Long goodsId, ModelMap modelMap) {
		try {

			Goods goods = goodsService.selectGoodsById(goodsId);

			modelMap.addAttribute("goods", goods);

			ModelAndView modelAndView = new ModelAndView("goodsList", modelMap);

			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

	/**
	 * 通过商家id 查询商品信息集合
	 * 
	 * @param merchantId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getGoodsByMerchantId.shtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView getGoodsByMerchantId(Integer merchantId, ModelMap modelMap) {
		try {

			List<Goods> goodsList = goodsService.selectGoodsByMerchantId(merchantId);

			modelMap.addAttribute("goodsList", goodsList);

			ModelAndView modelAndView = new ModelAndView("goodsList", modelMap);

			return modelAndView;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ConstantCode.FAIL.printMsg() + "," + e.getMessage());
		}
		return null;
	}

}
